#include "CommonDevice.h"
#include "Config.h"

#include "Drivers/Null/NullDriver.h"
#include "scene/SceneManager.h"
#include "InputManager.h"
#include "FileSystem.h"
#include "GUI/GUI.h"
#include "StateManager.h"
#include "ConfigManager.h"
#include "RandomGenerator.h"
#include "UsefulFunctions.h"
#include "Log.h"
#include "TimerManager.h"
#include "PackageManager.h"

CCommonDevice::CCommonDevice(StartDriverParams *DrvParams):IDevice(DrvParams)
{
}

CCommonDevice::~CCommonDevice()
{
	DeleteSceneManager();
	DeleteFileSystem();
	DeleteInputManager();
	DeleteVideoDriver();
	DeleteTextureManager();
	DeleteTimeManager();
	DeleteGUI();
	DeleteStateManager();
	DeleteConfigManager();
	DeleteRandomGenerator();
	DeleteUsefulFunctions();
	DeletePackageManager();
	DeleteLog();
}

void CCommonDevice::CreateHelpClasses()
{
	SetUsefulFunctions(new CUsefulFunctions);

	SetLog(new CLog());
	Log->SetActivesLogs(LOG_ALL);

	WriteInit();

	SetInputManager(new CInputManager());
	SetTimeManager(new CTimerManager());
	SetFileSystem(new CFileSystem());
	SetGUI(new CGUI());
	SetStateManager(new CStateManager());
	SetConfigManager(new CConfigManager());
	SetRandomGenerator(new CRandomGenerator());
	SetSceneManager(new CSceneManager(vector3dvpos(0.0,0.0,0.0),vector3dvpos(1.0,1.0,1.0),vector3dvpos(0.0,0.0,0.0)));
	SetPackageManager(new CPackageManager());
}

void CCommonDevice::WriteInit()
{
	pLogInfo << "Initializing PowerLibraryEngine ver. " << _LIBRARY_VERSION_ << "." << _LIBRARY_SUBVERSION_ "." << _LIBRARY_REVISION_ << "..." << endl;
}

void CCommonDevice::CreateCommonWindow()
{
	CreatePLEWindow();
	CreateDriver();

	ScreenLen.X = drvp->LenX;
	ScreenLen.Y = drvp->LenY;
}

void CCommonDevice::CreateNullDriver()
{
    CNullDriver *Temp = new CNullDriver(drvp);
    SetVideoDriver(Temp);
}

void CCommonDevice::CreateDriver()
{
	switch (drvp->drvType)
	{
#ifdef __DEFAULTDRIVER_OPENGL_
		default:
		case DRV_USEDEFAULT:
#endif
		case DRV_OPENGL:
		{
		    CreateOpenGLDriver();
		}
		break;

#ifdef __DEFAULTDRIVER_NULL_
		default:
		case DRV_USEDEFAULT:
#endif
		case DRV_NULLDRIVER:
		{
		    CreateNullDriver();
		}
		break;
	}
}

void CCommonDevice::PreUpdate()
{
	InputManager->UpdateKeys();
}

bool CCommonDevice::PostUpdate()
{
	TimeManager->Update();

	return false;
}
